﻿#region license
/*****************************************
*CopyRight (c) 2009-2013 HangJing Teconology. All Rights Reserved.
*Function :
*Created by jijunjian at 2013/9/16 21:58:13.
*E-Mail: jijunjian@ihangjing.com
*****************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;
using Hangjing.Model;
using Hangjing.SQLServerDAL;

namespace Hangjing.Weixin
{
    /// <summary>
    /// 获取凭证
    /// </summary>
    public class ACCESSTOKEN
    {
        protected HttpContext context;
        public ACCESSTOKEN(HttpContext _context)
        {
            context = _context;
        }

        /// <summary>
        /// 获取token
        /// </summary>
        /// <returns></returns>
        public string getAccessTokern()
        { 
            string token = "";
            string AppId = "";
            string AppSecret = "";
            WeiXinAccountInfo weixinaccount = CacheHelper.GetWeiXinAccount();

            AppId = weixinaccount.AppId;
            AppSecret = weixinaccount.AppSecret;


            string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+AppId+"&secret="+AppSecret;
            HttpHelper objhttp = new HttpHelper();
            objhttp.isToLower = false;
            HttpItem objHttpItem = new HttpItem()
            {
                URL = url,
                Encoding = "utf-8",
                Method = "GET",
                UserAgent = context.Request.UserAgent,
            };
            string returnmsg = objhttp.GetHtml(objHttpItem);

            JObject jo = JObject.Parse(returnmsg);
            token = jo["access_token"].ToString().Replace("\"","");
            //Hangjing.Common.HJlog.toLog("token="+token);

            return token;
        }
    }
}
